# -*- coding: utf-8 -*-
from django import forms
from django.contrib.auth.models import User
from django.contrib.admin import widgets
from wikicamp.worklist.models import *
from django.forms import ModelForm,HiddenInput,Textarea,DateInput,TextInput
from wikicamp.worklist.fields import DemirbasField

import datetime

class AtolyelerForm(forms.Form):
    id = forms.IntegerField(widget=HiddenInput,initial='0')
    sno= forms.IntegerField(label=u'Sıra No ')
    aciklama= forms.CharField(max_length=150,label=u'Atölye Adı ')
    durum_id = forms.ModelChoiceField(queryset=gecerlilik_durumlar.objects.all(),label='Durumu ')

class CaritipForm(forms.Form):
    id = forms.IntegerField(widget=HiddenInput,initial='0')
    sno = forms.IntegerField(label=u'Sıra No ')
    aciklama = forms.CharField(max_length=50,label=u'Açıklama ')
   

class CarikartForm(forms.Form):
    id = forms.IntegerField(widget=HiddenInput,initial='0')
    sno = forms.IntegerField(label=u'Sıra No ')
    aciklama = forms.CharField(max_length=50,label=u'Açıklama ')
    adres = forms.CharField(required=False,initial=False,widget=forms.widgets.Textarea(attrs={'cols':50,'rows':3}),max_length=450,label=u'Adres ')
    tel1 = forms.CharField(required=False,initial=False,max_length=20,label=u'Tel-1 ')
    tel2 = forms.CharField(required=False,initial=False,max_length=20,label=u'Tel-2 ')
    iletisim = forms.CharField(required=False,initial=False,max_length=150,label='İletişime Geçilecek Kişi ')
    email = forms.EmailField(required=False,initial=False,max_length=200,label='E-mail ')
    tip_id = forms.ModelChoiceField(queryset=caritip.objects.all(),label='Tip ')

class AidiyetForm(forms.Form):
    id = forms.IntegerField(widget=HiddenInput,initial='0')
    sno = forms.IntegerField(label=u'Sıra No ')
    aciklama = forms.CharField(max_length=50,label=u'Açıklama ')

        
class CihazlarForm(forms.Form):
    id = forms.IntegerField(widget=HiddenInput,initial='0')
    sno = forms.IntegerField(label=u'Sıra No ')
    aciklama = forms.CharField(max_length=100,widget=forms.TextInput(attrs={'size':40,'onblur':'umdn_getir();'}),label=u'Cihaz Adı ')
    serino = forms.CharField(max_length=100,label='Seri No ')
    lokasyon_id = forms.ModelChoiceField(queryset=lokasyonlar.objects.all().order_by('aciklama'),label=u'Bulunduğu Yer ')
    durum_id = forms.ModelChoiceField(queryset=gecerlilik_durumlar.objects.filter(),label='Durumu ')
    demirbasno = forms.CharField(max_length=100,label=u'Demirbaş No ')
    #demirbasno = DemirbasField()
    markasi_id= forms.ModelChoiceField(queryset=carikart.objects.filter(tip=caritip.objects.filter(id=1)).order_by('aciklama'),label=u'Markası ')
    modeli = forms.CharField(required=False,max_length=150,label=u'Modeli ')
    eknot= forms.CharField(required=False,widget=forms.widgets.Textarea(attrs={'cols':50,'rows':2}),max_length=200,label=u'Ek not ')
    parent_id = forms.ModelChoiceField(required=False,queryset=cihazlar.objects.filter().order_by('aciklama'),label=u'Bağlı Olduğu Cihaz ')
    aidiyet_id = forms.ModelChoiceField(queryset=aidiyet.objects.filter(),label=u'Aidiyeti ')
    barkod = forms.CharField(required=False,max_length=100,label=u'Barkodu ')
    #garanti_bitis_tarihi = forms.DateTimeField(required=False,widget = forms.widgets.DateTimeInput(attrs={'size':15,}),label= u'Garanti Bitiş Tarihi ')
    garanti_bitis_tarihi = forms.DateTimeField(required=False,widget=widgets.AdminDateWidget(),label=u'Garanti Bitiş Tarihi ')
    yetkili_servisi_id= forms.ModelChoiceField(required=False,queryset=carikart.objects.filter(tip=caritip.objects.filter(id=3)),label=u'Yetkili Servisi ')
    cihaz_alinis_tarihi = forms.DateField(required=False,widget=widgets.AdminDateWidget(),label=u'Cihaz Alınış Tarihi ')
    
class EkCihazlarForm(forms.Form):
    cihaz_id = forms.ModelChoiceField(required=False,queryset=cihazlar.objects.filter(parent_id__isnull=True),label=u'Eklemek istediğiniz cihaz ')
    
class Gecerlilik_durumlarForm(forms.Form):
    id = forms.IntegerField(widget=HiddenInput,initial='0')
    sno = forms.IntegerField(label=u'Sıra No ')
    aciklama = forms.CharField(max_length=100,label='Durum Bilgisi ')

class LokasyonlarForm(forms.Form):
    id = forms.IntegerField(widget=HiddenInput,initial='0')
    sno = forms.IntegerField(label=u'Sıra No ')
    aciklama = forms.CharField(max_length=100,label='Lokasyonu ')
    durum_id = forms.ModelChoiceField(queryset=gecerlilik_durumlar.objects.all(),label='Durumu ')
    
class IsturuForm(forms.Form):
    id = forms.IntegerField(widget=HiddenInput,initial='0')
    sno = forms.IntegerField(label=u'Sıra No ')
    aciklama= forms.CharField(max_length=150,label=u'İş Türü ')

class Is_durumlarForm(forms.Form):
    id = forms.IntegerField(widget=HiddenInput,initial='0')
    sno = forms.IntegerField(label=u'Sıra No ')
    aciklama= forms.CharField(max_length=100,label=u'İş Durumu ')

class UserForm(forms.ModelForm):
   
    class Meta:
        model = User
        exclude = ['username','password','is_staff','is_active','is_superuser','groups','date_joined','last_login','user_permissions']

class ProfileForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        # magic 
        #print "loyloy : " + str(kwargs) + " tipi1 : " + str(type(kwargs)) + " - bilem : " + str(args) +" tipi : " + str(type(args))
        self.user = kwargs['instance'].user
        user_kwargs = kwargs.copy()
        user_kwargs['instance'] = self.user
        self.uf = UserForm(*args, **user_kwargs)
        # magic end 
        #print "loy: " + str(self.uf)
        super(ProfileForm, self).__init__(*args, **kwargs)
        #print "fffs : " + str(self.fields)
        self.fields.update(self.uf.fields)
        self.initial.update(self.uf.initial)
        #print "fffs 2: " + str(self.fields)
        # define fields order if needed
        self.fields.keyOrder = ('first_name','last_name','unvan','email','atolye','ack',)
       
    def save(self, *args, **kwargs):
        # save both forms   
        self.uf.save(*args, **kwargs)
        return super(ProfileForm, self).save(*args, **kwargs)

    class Meta:
        model = kullanicilar
        fields = ['ack','unvan','atolye']
        widgets =  {  
                    'ack': Textarea(attrs={'cols': 120, 'rows': 3}), 
        }
                 
        
class KullanicilarForm(forms.Form):
    id= forms.IntegerField(widget=HiddenInput,initial='0')
    sno = forms.IntegerField(label=u'Sıra No ')
    user_id = forms.IntegerField(widget=HiddenInput,label=u'Kullanıcı Adı ')
    unvan = forms.CharField(max_length=20,label=u'Unvan :')
    ack =  forms.CharField ( widget=forms.widgets.Textarea(attrs={'cols':120,'rows':3}),max_length=250,label=u'Kullanıcı Adı ' )
    atolye_id = forms.ModelMultipleChoiceField(queryset=atolyeler.objects.all(),label=u'Atolyesi :')
    timestamp = forms.DateTimeField(widget=HiddenInput,initial=datetime.datetime.now())
    

class KullanicigrupForm(forms.Form):
    id = forms.IntegerField(widget=HiddenInput,initial='0')
    sno = forms.IntegerField(label=u'Sıra No ')
    aciklama = forms.CharField(max_length=150,label=u'Kullanıcı Grup ')
    durum_id = forms.ModelChoiceField(queryset=gecerlilik_durumlar.objects.all(),label='Durumu ')
    
class islistesiForm(forms.ModelForm):
    id = forms.IntegerField(widget=HiddenInput,initial='0')
    sno = forms.IntegerField(widget=forms.TextInput(attrs={'size':5,'readonly':True}),label=u'Sıra No ')
    emailbildirimleri = forms.BooleanField(required=False,initial=False,label=u'Bu iş ile ilgili bilgilendirmeler email ile yapılsın mı? ')
    onaylandi = forms.BooleanField(required=False,initial=False,label=u"Bu İş İstek Kapatılsın ")
    turu = forms.ModelChoiceField(queryset=isturu.objects.filter().exclude(id__in=(4,6)),label=u'Arıza Türü ')
    aciklama = forms.CharField(widget=forms.widgets.Textarea(attrs={'cols':50,'rows':3}),max_length=250,label=u'Ek açıklamalar ')
    eknot = forms.CharField(required=False,initial=False,widget=forms.widgets.Textarea(attrs={'cols':50,'rows':3}),max_length=250,label=u'Onaylayan Notları ')
    planlanan_baslangic_tarihi = forms.DateTimeField(widget=forms.widgets.DateTimeInput(attrs={'size':15,}),label= u'İş Başlangıç ')
    planlanan_bitis_tarihi = forms.DateTimeField(required=False,initial=False,widget = forms.widgets.DateTimeInput(attrs={'size':15,}),label= u'Planlanan Bitiş Tarihi ')
    class Meta:
        model = islistesi
        exclude = ['kullanici','timestamp']
        
class BakimForm(ModelForm):
    aciklama = forms.CharField(max_length=100,label=u"Başlık ")
    tumgun = forms.BooleanField(required=False,initial=False,label=u"Tüm Gün? ")
    turu = forms.ModelChoiceField(queryset=isturu.objects.filter(id__in=(4,6)),label=u'Bakım Türü ')
    class Meta:
        model = islistesi
        exclude = ('sno','emailbildirimleri','lokasyon','cihaz','kullanici','onaylayan_kullanici','onaylandi','timestamp','islem_durum','eknot','url')

class BakimnoktalariForm(forms.Form):
    id = forms.IntegerField(widget=HiddenInput,initial='0')
    sno = forms.IntegerField(label=u'Sıra No ')
    aciklama= forms.CharField(widget=forms.widgets.TextInput(attrs={'size':40}),max_length=350,label=u"Açıklama ")
    
class BakimtaslagiForm(ModelForm):
    
    class Meta:
        model = bakimtaslagi

class FaturaForm(ModelForm):
    #fatura_tarih = forms.DateField(widget=forms.widgets.DateInput(attrs={'size':10}),label=u"Fat. Tarih")
    fatura_tarih= forms.DateField(widget=forms.widgets.DateInput(attrs={'size':10}))
    fatura_tarih= forms.CharField(widget=forms.widgets.TextInput(attrs={'size':10}))

    class Meta:
        model = fatura
        exclude = ['sno','kullanici','timestamp','transfer_edildi','transfer_kul','transfer_timestamp']


        
class FaturadetayForm(ModelForm):
    aciklama = forms.CharField(widget=forms.widgets.TextInput(attrs={'size':75,'maxlength':150}))
    quantity = forms.IntegerField(widget=forms.widgets.TextInput(attrs={'size':3,'maxlength':4}))
    price = forms.CharField(widget=forms.widgets.TextInput(attrs={'size':10,'alt':u"formatWhileTypingAndWarnOnDecimalsEntered2",'title':u"KDV hariç"}))
    unit = forms.ModelChoiceField(queryset=birim.objects.filter())
    
    class Meta:
        model = fatura_detay
        exclude = ['kullanici','timestamp','fatura','cost_price','price_total','tax_total','discount_rate1','discount_total1','discount_rate2','discount_total2','coefficient']
         
class DocumentForm(ModelForm):
    aciklama = forms.CharField(widget=forms.widgets.TextInput(attrs={'size':100,'maxlength':250}))
    cihaz = forms.ModelChoiceField(queryset=cihazlar.objects.filter(),widget=forms.widgets.Select(attrs={'multiple':'multiple', 'size':25, 'style':"width:480px;"}))
    class Meta:
        model = Document
        exclude = ('timestamp','kullanici','tag','cihaz')

class TuketimnoktalariForm(forms.Form):
    id = forms.IntegerField(widget=HiddenInput,initial='0')
    sno = forms.IntegerField(label=u'Sıra No ')
    aciklama= forms.CharField(widget=forms.widgets.TextInput(attrs={'size':40}),max_length=350,label=u"Açıklama ")
    birim_id = forms.ModelChoiceField(queryset=birim.objects.filter(),label=u"Birim ")

class TuketimtablosuForm(ModelForm):
    tarih=forms.DateField(required=False,widget=forms.widgets.DateInput(attrs={'size':12}),label=u'Cihaz Alınış Tarihi ')
    class Meta:
        model = tuketimtablosu
        exclude= ('timestamp','kullanici','kayit')